package com.insurance.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Optional;

public class AmountUtils {

    private static final BigDecimal HUNDRED = new BigDecimal(100);
    private static final BigDecimal ZERO = new BigDecimal(0);

    private static final BigDecimal TEN_THOUSAND = new BigDecimal(10000);
    private static final BigDecimal ONE_MILLION = new BigDecimal(1000000);

    /**
     * 金额分转元
     * @param money
     * @return
     */
    public static BigDecimal fenToYuanBigDecimal(Long money) {
        if (money != null) {
            return new BigDecimal(money).divide(HUNDRED, 2, BigDecimal.ROUND_HALF_UP);
        } else {
            return ZERO;
        }
    }

    /**
     * 金额分转万
     * @param money
     * @return
     */
    public static BigDecimal fenToWanBigDecimal(Long money) {
        if (money != null) {
            return new BigDecimal(money).divide(ONE_MILLION, 2, BigDecimal.ROUND_HALF_UP);
        } else {
            return ZERO;
        }
    }

    /**
     * 金额分转元
     * @param money
     * @return
     */
    public static String fenToYuanString(Long money) {
        return Optional.ofNullable(money)
                .map(AmountUtils::fenToYuanBigDecimal)
                .map(BigDecimal::toString)
                .orElse(null);
    }

    /**
     * 金额元转分
     * @param money
     * @return
     */
    public static Long yuanTofenLong(Long money) {
        if (money != null) {
            return new BigDecimal(money).multiply(HUNDRED).longValue();
        } else {
            return ZERO.longValue();
        }
    }

    /**
     * 金额元转分
     * @param money
     * @return
     */
    public static long yuanToFenLong(BigDecimal money) {
        return Optional.ofNullable(money)
                    .map(bd -> bd.setScale(2, BigDecimal.ROUND_HALF_UP))
                    .map(y -> y.multiply(HUNDRED))
                    .map(BigDecimal::longValue)
                    .orElse(0L);
    }

    public static Long yuanToFenLong(String money) {
        if(StringUtils.isBlank(money)){
            return null;
        }
        return Optional.ofNullable(new BigDecimal(money))
                    .map(bd -> bd.setScale(2, BigDecimal.ROUND_HALF_UP))
                    .map(y -> y.multiply(HUNDRED))
                    .map(BigDecimal::longValue)
                    .orElse(0L);
    }

    /**
     * 金额万转分
     * @param money
     * @return
     */
    public static long wanToFenLong(BigDecimal money) {
        return Optional.ofNullable(money)
                .map(bd -> bd.setScale(2, BigDecimal.ROUND_HALF_UP))
                .map(y -> y.multiply(ONE_MILLION))
                .map(BigDecimal::longValue)
                .orElse(0L);
    }

    public static Long wanToFenLong(String money) {
        if(StringUtils.isBlank(money)){
            return null;
        }
        return Optional.ofNullable(new BigDecimal(money))
                .map(bd -> bd.setScale(2, BigDecimal.ROUND_HALF_UP))
                .map(y -> y.multiply(ONE_MILLION))
                .map(BigDecimal::longValue)
                .orElse(0L);
    }

    public static void main(String[] args) {
        long fen = 50;
        System.out.println(wanToFenLong(new BigDecimal(50)));
    }
}
